package com.xoa.dev.pz.service.impl;

import com.xoa.dev.pz.dao.*;
import com.xoa.dev.pz.model.Pzstatic;
import com.xoa.dao.party.GridInfoMapper;
import com.xoa.model.party.GridInfo;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * Created by liuqian on 2017/12/8.
 */
@Service
public class PzStatisticsService {

    @Resource
    PzFullReleaseMapper pzFullReleaseMapper;
    @Resource
    PzCorrectionMapper pzCorrectionMapper;
    @Resource
    PzEasyAccidentMapper pzEasyAccidentMapper;
    @Resource
    PzDrugMapper pzDrugMapper;
    @Resource
    PzKeyYouthMapper pzKeyYouthMapper;
    @Resource
    PzKeyPetitionMapper pzKeyPetitionMapper;
    @Resource
    PzPersonMapper pzPersonMapper;
    @Resource
    private GridInfoMapper gridInfoMapper;



    public ToJson<Object> getPersonCount(HttpServletRequest request){
      ToJson<Object> json =new ToJson<Object>();
        try {
            Map<String,Object> map =new HashedMap();
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH)+1;
            Map<String,Object> paraMap =new HashedMap();
            paraMap.put("year",year);
            paraMap.put("month",month);
            int i = pzFullReleaseMapper.selectCount(paraMap);
            int i1 = pzCorrectionMapper.selectperSonCount(paraMap);
            int i2 = pzEasyAccidentMapper.selectCount(paraMap);
            int i3 = pzDrugMapper.selectCount(paraMap);
            int i4 = pzKeyYouthMapper.selectCount(paraMap);
            int i5 = pzKeyPetitionMapper.selectCount(paraMap);
            //本月录有特殊人群数据
            int i6 = pzPersonMapper.selectPersonCount(paraMap);
            Map<String,Object> paraMap2 =new HashedMap();
            paraMap2.put("year",year);
            paraMap2.put("month",month-1);
            //上月录有特殊人群数据
            int i7 =pzPersonMapper.selectPersonCount(paraMap2);
            //较上月增长i8人
            int i8=i6-i7;
            //环比率计算法：本次数据-上次数据/上次数据
            String Circular="";
            if(i7!=0){
                double q=(i6-i7)/i7;
                NumberFormat num = NumberFormat.getPercentInstance();
                String rates = num.format(q);
                Circular=String.valueOf(rates);
            }else{
                 Circular=String.valueOf(i6);
            }



            //较同期增长
            Map<String,Object> paraMap3 =new HashedMap();
            List<Object> list4= new ArrayList<>();
            paraMap3.put("year",year-1);
            paraMap3.put("month",month);
            int i9 = pzPersonMapper.selectPersonCount(paraMap3);
            int ii=i6-i9;
            //同比增长率
            String growthrate="";
            if(i9!=0){
                double keyvalue= (i6-i9)/i9;
                NumberFormat num = NumberFormat.getPercentInstance();
                String rates = num.format(keyvalue);
                 growthrate=String.valueOf(rates);
            }else{
                growthrate=String.valueOf(i6);
            }

            List<GridInfo> list=gridInfoMapper.gridSettingsTree("0");
            List<GridInfo> gridInfoList=new ArrayList<>();
            for(GridInfo grid:list){
                if(!StringUtils.checkNull(grid.getUpperGrid())){
                    gridInfoList.addAll(gridInfoMapper.gridSettingsTree(grid.getGridId().toString()));
                }
            }


            for(int j=0;j<gridInfoList.size();j++){
                Map<String,Object> paraMap4 =new HashedMap();
                Map<String,Object> paraMap5 =new HashedMap();
                int personNumpre=0;
                int personNumafter=0;
                paraMap4.put("year",year);
                paraMap4.put("month",month-1);
                paraMap4.put("pGrid",gridInfoList.get(j).getGridId());
                paraMap5.put("year",year);
                paraMap5.put("month",month);
                paraMap5.put("pGrid",gridInfoList.get(j).getGridId());
                personNumpre=pzPersonMapper.selPerNumByGrid(paraMap4);
                personNumafter=pzPersonMapper.selPerNumByGrid(paraMap5);
                String  personNumrete="";
                if(personNumpre!=0){
                    int keyvalue= (personNumafter-personNumpre)/personNumpre;
                    personNumrete=String.valueOf(keyvalue);
                }
                else{
                    personNumrete=String.valueOf(personNumafter);
                }
                Map<String,Object> mapj= new HashedMap();
                mapj.put("pre",personNumpre);
                mapj.put("after",personNumafter);
                mapj.put("rate",personNumrete);
                mapj.put("name",gridInfoList.get(j).getGridName());
                map.put("map"+j,mapj);
            }
            Pzstatic pzstatic =new Pzstatic();
           pzstatic.setPzFull(String.valueOf(i));
            pzstatic.setPzDrug(String.valueOf(i3));
           pzstatic.setPzCorr(String.valueOf(i1));
           pzstatic.setPzEasy(String.valueOf(i2));
            pzstatic.setCircular(Circular);
            pzstatic.setCount(String.valueOf(i6));
            pzstatic.setDifferencemonth(String.valueOf(i8));
            pzstatic.setDifferenceyear(String.valueOf(ii));
            pzstatic.setGrowthrate(growthrate);
            pzstatic.setPzKeyYoung(String.valueOf(i4));
            pzstatic.setPzKeyPet(String.valueOf(i5));
            list4.add(pzstatic);
            list4.add(map);
            json.setFlag(0);
            json.setMsg("ok");
            json.setObject(list4);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017-12-11 上午12:33:59
     * 参数说明:	   特殊人群分析研判第二个接口
     * 参数说明:   @return
     */
    public ToJson<Object> getVisitCount(HttpServletRequest request){
        ToJson<Object> json =new ToJson<Object>();
        try {
            List<Object> returnlist =new ArrayList<>();
           ;
            List<GridInfo> list=gridInfoMapper.gridSettingsTree("0");
            List<GridInfo> gridInfoList=new ArrayList<>();
            for(GridInfo grid:list){
                if(!StringUtils.checkNull(grid.getUpperGrid())){
                    gridInfoList.addAll(gridInfoMapper.gridSettingsTree(grid.getGridId().toString()));
                }
            }
            for(int j=0;j<gridInfoList.size();j++){
                Map<String,Object> map =new HashedMap();
                int noVisitCount=0;
                int visitCount=0;
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH)+1;
                Map<String,Object> paraMap =new HashedMap();
                paraMap.put("year",year);
                paraMap.put("month",month);
                paraMap.put("grid",gridInfoList.get(j).getGridId());
                noVisitCount = pzPersonMapper.selectNoVistCount(paraMap);
                visitCount = pzPersonMapper.selectVistCount(paraMap);
                map.put("name",gridInfoList.get(j).getGridName());
                map.put("noVisitCount",noVisitCount);
                map.put("visitCount",visitCount);
                returnlist.add(map);
            }
            json.setMsg("ok");
            json.setFlag(0);
            json.setObject(returnlist);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017-12-12 上午12:33:59
     * 参数说明:	   特殊人群分析研判第三个接口
     * 参数说明:   @return
     */
    public ToJson<Object> getAnalysisCount(HttpServletRequest request){
        ToJson<Object> json =new ToJson<Object>();
        try {
            Map<String,Object> map =new HashedMap();
            int i = pzPersonMapper.selectCount();
            List<Object> list =new ArrayList<>();
            int num1 = pzFullReleaseMapper.selectTotalCount();
            int num2 = pzCorrectionMapper.selectTotalCount();
            int num3 = pzEasyAccidentMapper.selectTotalCount();
            int num4 = pzDrugMapper.selectTotalCount();
            int num5 = pzKeyYouthMapper.selectTotalCount();
            int num6 = pzKeyPetitionMapper.selectTotalCount();
            Map<String,Object> paraMap1 =new HashedMap();
            Map<String,Object> paraMap2 =new HashedMap();
            Map<String,Object> paraMap3 =new HashedMap();
            Map<String,Object> paraMap4 =new HashedMap();
            Map<String,Object> paraMap5 =new HashedMap();
            Map<String,Object> paraMap6 =new HashedMap();
            paraMap1.put("count",num4);
            paraMap1.put("type","吸毒人员");
            paraMap2.put("count",num3);
            paraMap2.put("type","精神障碍患者");
            paraMap3.put("count",num2);
            paraMap3.put("type","社区矫正人员");
            paraMap4.put("count",num5);
            paraMap4.put("type","重点青少年");
            paraMap5.put("count",num6);
            paraMap5.put("type","重点信访人员");
            paraMap6.put("count",num1);
            paraMap6.put("type","刑满释放人员");
            list.add(paraMap1);
            list.add(paraMap2);
            list.add(paraMap3);
            list.add(paraMap4);
            list.add(paraMap5);
            list.add(paraMap6);
            json.setFlag(0);
            json.setMsg("ok");
            json.setObject(list);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
      return json;
    }

}
